﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace StockCommon.data
{
    public class StockEventTags
    {
        List<EventTag> todos = new List<EventTag>();
        public void flush()
        {
            if (todos.Count == 0)
                return;
            StockDataClassesDataContext context = getContext();
            foreach (EventTag et in todos)
            {
                EventTag t = (from e in context.EventTags where (e.日期 == et.日期 && e.代號 == et.代號 && e.Event == et.Event) select e).FirstOrDefault<EventTag>();
                if (t != null)
                    continue;
                et.id = NewGuid.create("even"); ;
                context.EventTags.InsertOnSubmit(et);
                context.SubmitChanges();
            }
            todos.Clear();
        }
        private static StockDataClassesDataContext getContext()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            return context;
        }
        protected void setEvent(String code, DateTime date, String Event)
        {
            EventTag et = new EventTag();
            et.id = NewGuid.create("even");
            et.代號 = code;
            et.日期 = date;
            et.Event = Event;
            todos.Add(et);
        }

        public static String 除息 = "CashDividend";
        public static String 除權 = "CashDividend";
        

        public void setCashDividend(String code, DateTime date)
        {
            setEvent(code, date,StockEventTags.除息);
        }
        public void setStockDividend(String code, DateTime date)
        {
            setEvent(code, date,StockEventTags.除權);
        }
        public void setLimitUP_Start(String code, DateTime date)
        {
            setEvent(code, date, "LimitUpStart");
        }
        public void setLimitUP_End(String code, DateTime date)
        {
            setEvent(code, date, "LimitUpEnd");
        }
        public void setLimitUP_Max(String code, DateTime date)
        {
            setEvent(code, date, "LimitUpMax");
        }
        public void setLimitUP_Min(String code, DateTime date)
        {
            setEvent(code, date, "LimitUpMin");
        }
        public void setLimitDOWN_Start(String code, DateTime date)
        {
            setEvent(code, date, "LimitDownStart");
        }
        public void setLimitDOWN_End(String code, DateTime date)
        {
            setEvent(code, date, "LimitDownEnd");
        }
        public void setLimitDOWN_Max(String code, DateTime date)
        {
            setEvent(code, date, "LimitDownMax");
        }
        public void setLimitDOWN_Min(String code, DateTime date)
        {
            setEvent(code, date, "LimitDownMin");
        }

    }
}
